package com.xb.loan.app.admin;
import com.qicai.common.page.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import AdminConstants;
import com.qicai.service.common.dto.AdminUserDTO;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import com.xb.loan.app.service.TLoanPlatformService;
import com.xb.loan.app.dto.TLoanPlatformDTO;
import static AdminConstants.SESSION_USER_KEY;
import com.qicai.service.common.util.SessionUtils;
/**
 *  贷款平台控制器
 * @author   wuql
 * @version tLoanPlatformService.java, v 0.1  2017-11-6 15:10:01
 */
@Controller
@RequestMapping(value = "/admin/tLoanPlatform", method = {RequestMethod.GET, RequestMethod.POST})
public class TLoanPlatformController {
	private static final Logger logger = LoggerFactory.getLogger(TLoanPlatformController.class);
	/**贷款平台业务接口**/
        @Resource
        private TLoanPlatformService tLoanPlatformService;
        @RequestMapping(value = "/list", method = {RequestMethod.GET, RequestMethod.POST})
        public String list(HttpServletRequest request, @RequestParam(value = "currentPage", required = false, defaultValue = "0") int currentPage,
                           Model view) {
            try {
                logger.debug("tLoanPlatform.list start");
                TLoanPlatformDTO tLoanPlatform =new TLoanPlatformDTO();
                tLoanPlatform.setPage(currentPage);
            Page<TLoanPlatformDTO> page = tLoanPlatformService.queryTLoanPlatformPage(tLoanPlatform);
                logger.debug("tLoanPlatform.list pageResult:{}", page.getResult());

                //放入page对象。
                view.addAttribute("page", page);
                logger.debug("tLoanPlatform.list end");
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
            }
            return "tLoanPlatform-list";
        }

        @RequestMapping(value = "/edit", method = {RequestMethod.PUT})
        public String edit(@RequestParam(value = "id", required = false, defaultValue = "0") Long id,
                           Model view) {
            logger.debug("tLoanPlatform.edit start param.id={}", id);
            try {
                if (id != null && id > 0) {
                    TLoanPlatformDTO tLoanPlatform = tLoanPlatformService.queryTLoanPlatformById(id);
                    view.addAttribute("tLoanPlatform", tLoanPlatform);
                }
                logger.debug("tLoanPlatform.edit end");
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
            }
            return "tLoanPlatform-edit";
        }

        @RequestMapping(value = "/save", method = {RequestMethod.POST})
        @ResponseBody
        public String save(HttpServletRequest request, TLoanPlatformDTO tLoanPlatform,
                           Model view) {
            logger.debug("tLoanPlatform.save start param.tLoanPlatform={}", tLoanPlatform);
            try {
                AdminUserDTO sessionUser = SessionUtils.getSessionValue(request, SESSION_USER_KEY);
                if (tLoanPlatform != null) {
                    tLoanPlatform.setCreatorId(sessionUser.getId());
                    long rows = tLoanPlatformService.saveTLoanPlatform(tLoanPlatform);
                    logger.debug("tLoanPlatform.save end");
                    if (rows > 0) {
                        return String.format(AdminConstants.WEB_IFRAME_SCRIPT, "保存成功！");
                    } else {
                        return String.format(AdminConstants.WEB_IFRAME_SCRIPT, "保存失败！");
                    }
                }

            } catch (Exception e) {
                logger.error(e.getMessage(), e);
                return String.format(AdminConstants.WEB_IFRAME_SCRIPT, "保存失败！");
            }
            return String.format(AdminConstants.WEB_IFRAME_SCRIPT, "保存成功！");
        }

        @RequestMapping(value = "/delete", method = {RequestMethod.DELETE})
        @ResponseBody
        public String delete(@RequestParam(value = "id", required = false, defaultValue = "0") Long id,
                             Model view) {
            logger.debug("tLoanPlatform.delete start param.id={}", id);
            try {
                long rows = tLoanPlatformService.deleteTLoanPlatform(id);
                logger.debug("tLoanPlatform.delete end");
                if (rows > 0) {
                    return String.format(AdminConstants.WEB_IFRAME_SCRIPT, "删除成功！");
                } else {
                    return String.format(AdminConstants.WEB_IFRAME_SCRIPT, "删除失败！");
                }
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
                return String.format(AdminConstants.WEB_IFRAME_SCRIPT, "删除失败！");
            }
        }





}